/**
 * @param {String} str (y-m-d h:i:s) y:年 m:月 d:日 h:时 i:分 s:秒
 */

function dateTimeStr(str) {
  var date = new Date(),
    year = date.getFullYear(), //年
    month = date.getMonth() + 1, //月
    day = date.getDate(), //日
    hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(), //时
    minute = date.getMinutes() < 10 ? date.getMinutes() : date.getMinutes(), //分
    second = date.getSeconds() < 10 ? date.getSeconds() : date.getSeconds(); //秒
  month >= 1 && month <= 9 ? (month = "0" + month) : "";
  day >= 0 && day <= 9 ? (day = "0" + day) : "";
  hour >= 0 && hour <= 9 ? hour : "";
  minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
  second >= 0 && second <= 9 ? (second = "0" + second) : "";
  if (str.indexOf('y') != -1) {
    str = str.replace('y', year)
  }
  if (str.indexOf('m') != -1) {
    str = str.replace('m', month)
  }
  if (str.indexOf('d') != -1) {
    str = str.replace('d', day)
  }
  if (str.indexOf('h') != -1) {
    str = str.replace('h', hour)
  }
  if (str.indexOf('i') != -1) {
    str = str.replace('i', minute)
  }
  if (str.indexOf('s') != -1) {
    str = str.replace('s', second)
  }
  return str;
}

//获取当前时间戳
function dtime() {
  var date = new Date()
  let time = date.getTime();
  return time
}
function timestampToTime(timestamp) {
  // 时间戳为10位需*1000，时间戳为13位不需乘1000
  var date = timestamp?new Date(timestamp * 1000):new Date(this.dtime());
  console.log(date)
  var Y = date.getFullYear() + "-";
  var M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + "-";
  var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
  var h = date.getHours() + ":";
  var m = date.getMinutes() + ":";
  var s = date.getSeconds();
  return Y + M + D + h + m + s;
}

//获取两个时间之间的差额
function getInervalHour(startDate, endDate) {
  let start = startDate
  let end = endDate
  var ms =end - start;
 if(ms=="NaN"){
	
		  return 0 +'分';
	}
	
  if(ms<0){
     return 0 +'分';
  }
  if (ms / 1000 / 60 / 60 < 1) {
    if(ms / 1000 / 60<1){
      return Math.floor(ms / 1000 ) + '秒';
    }
    return Math.floor((ms / 1000 / 60),2) + '分';
  } else {
	  let t=Math.floor(ms / 1000 / 60 / 60) + '小时'
	  
	  if(t==='NaN小时'){
		  return 0 +'分';
	  }
	  
    return Math.floor(ms / 1000 / 60 / 60) + '小时';
  }

}
//当前时间加分钟 
function timej(times, tiem) {
  
  let jia=parseInt(times)
  var dstime;
  if (!tiem) {
    dstime = new Date().setMinutes(new Date().getMinutes() + jia);
  } else {
   
    if(dtime()>tiem){
      dstime = new Date().setMinutes(new Date().getMinutes() + jia);
    }else{
      dstime = new Date(parseInt(tiem)).setMinutes(new Date(parseInt(tiem)).getMinutes() + jia);
    }
  } 
  return dstime
}
module.exports = {
  dateTimeStr: dateTimeStr,
  getInervalHour: getInervalHour,
  timej: timej,
  dtime: dtime,
  timestampToTime:timestampToTime
}
